<?php
require_once("../../inc/global.php");
include("function.php");
adminislogin();
$MenuID=$_GET["MenuID"];
if(empty($MenuID)||!is_numeric($MenuID)){
	$base->alert("参数不正确");
}
$html->includetop();
echo "\n<script type=\"text/javascript\" src=\"/inc/Calendar.js\"></script>\n当前位置：添加 >> {$System->GetMenuSysField($MenuID,1)}";
$MenuTableName=$System->GetTableName($MenuID);
//读取配置文件
$filepath="../{$MenuTableName}/{$MenuID}.tpl";
if(!file_exists($filepath)){
	$base->alert("表单模版没有找到，请重新保存一下本栏目设置！！");
}
$fp=fopen($filepath,"r");
$mobantpl=fread($fp,filesize($filepath));
fclose($fp);
//读取数据库配置
$getfromconfig=$System->GetMenuSysFormConfig($MenuID);
$getfromconfigarr=explode(",",$getfromconfig);
//替换
for($i=1;$i<count($getfromconfigarr);$i++){
	$fromlist=explode("|",$getfromconfigarr[$i]);
	if($fromlist[1]=="8"){
		$mobantpl=str_replace("{ykt:FileName_".$i."}",$MenuID,$mobantpl);
	}elseif($fromlist[1]=="15"){
		$mobantpl=str_replace("{ykt:FileName_".$i."}",time(),$mobantpl);
	}else{
		if($fromlist[1]=="2"){
			$mobantpl=str_replace("{ykt:FileName_{$i}}",is_numeric($fromlist[6])?$fromlist[6]:0,$mobantpl);
		}else{
			$mobantpl=str_replace("{ykt:FileName_{$i}}",$fromlist[6],$mobantpl);
		}
	}
}
$Save_url="sys_add_save.php?MenuID={$MenuID}&SuperAdd=1";
$vpattern= '/{#Class:(dropdownlist|radio|checkbox){1}:([0-9]+):(\w+)#}/i';
preg_match_all($vpattern,$mobantpl,$SArr);
//print_r($SArr);]
if(is_array($SArr)){
	for($j=0;$j<count($SArr[0]);$j++){
		if($SArr[1][$j]!=""){
			$d_Type=$SArr[1][$j];
			$d_ClassID=$SArr[2][$j];
			$d_FieldName=$SArr[3][$j];
			//读数据
			if($d_Type=="dropdownlist"){
				$optionitem="";
				$MenuIDFieldID=$System->GetFormConfigMenuIDFieldID($d_ClassID);  //获取MenuID所在字段序号
				$result=$db->query("select * from classsys where classsys{$MenuIDFieldID}={$d_ClassID}");
				$DatalistArr=$db->GetDatalist($result);
				$optionitem=$From->listboxsub($DatalistArr,0,0);
				$mobantpl=str_replace("{#Class:{$d_Type}:{$d_ClassID}:{$d_FieldName}#}",$optionitem,$mobantpl);
			}elseif($d_Type=="radio"){
				$radioitem="";
				$MenuIDFieldID=$System->GetFormConfigMenuIDFieldID($d_ClassID);  //获取MenuID所在字段序号
				$result=$db->query("select * from classsys where classsys{$MenuIDFieldID}={$d_ClassID}");
				while($row=$db->getarray($result)){
					$radioitem.="<input name=\"".$d_FieldName."\" type=\"radio\" value=\"".$row[0]."\">".$row[1]."</input>";
				}
				$mobantpl=str_replace("{#Class:{$d_Type}:{$d_ClassID}:{$d_FieldName}#}",$radioitem,$mobantpl);
			}elseif($d_Type=="checkbox"){
				$checkboxitem="";
				$MenuIDFieldID=$System->GetFormConfigMenuIDFieldID($d_ClassID);  //获取MenuID所在字段序号
				$result=$db->query("select * from classsys where classsys{$MenuIDFieldID}={$d_ClassID}");
				while($row=$db->getarray($result)){
					$checkboxitem.="<input name=\"".$d_FieldName."\" type=\"checkbox\" value=\"".$row[0]."\">".$row[1]."</input>";
				}
				$mobantpl=str_replace("{#Class:{$d_Type}:{$d_ClassID}:{$d_FieldName}#}",$checkboxitem,$mobantpl);	
			}
		}
	}
}
$mobantpl=str_replace("{ykt:Save_url}",$Save_url,$mobantpl);
echo $mobantpl;
echo "<script type=\"text/javascript\">document.getElementById(\"pilianadd\").style.display='';</script>";
$html->includeend(); 

?>